<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    html * {
      padding: 0;
      margin: 0;
    }
  </style>
</head>
<body>
  <style>
    .box {
      list-style: none;
      width: 300px;
      display: flex;
      flex-wrap: wrap;
    }
    .li-box {
      width: 100px;
      line-height: 100px;
      height: 100px;
      text-align: center;
      box-sizing: border-box;
      border: 4px solid black;
      margin-left: -4px;
      margin-top: -4px;
    }
    .li-box:nth-child(3n+1) {
      margin-left: 0;
    }
    .li-box:nth-child(-n+3) {
      margin-top: 0;
    }
    .li-box:hover{
      border: 4px solid red;
      z-index: 2;
    }
  </style>
  <ul class="box">
    <li class="li-box">1</li>
    <li class="li-box">2</li>
    <li class="li-box">3</li>
    <li class="li-box">4</li>
    <li class="li-box">5</li>
    <li class="li-box">6</li>
    <li class="li-box">7</li>
    <li class="li-box">8</li>
    <li class="li-box">9</li>
  </ul>
</body>
</html>